function [A,V,L,Q] = UpdateAV(X, U, L, S, Q, alpha, beta, mu, r, c)
% Update A 
%dictsize=k;
[d,n] = size(X);
In = eye(n);
tS = S.^r;
Qs=(tS+tS')/2;
Ls=diag(sum(Qs))-Qs;
M = In + n*beta/alpha * Ls;

AA = U' * U;   %B in the paper
tmp = bsxfun(@times, X, L); %L^{-1}*X
tmp1 = n*mu/alpha*alpha;
if d < n
  C1 = (tmp*M*X' + tmp1*eye(d))\(tmp);
else
  MXT = M*X'*tmp;
  C1 = tmp1^(-1)*tmp - tmp1^(-1)*tmp/(tmp1*In+MXT)*MXT; % 
end
C = X' * C1; 
C = (C + C') / 2;
tmp = C - In;
BB = alpha * (tmp * tmp')+eps;
BBQ = bsxfun(@times, BB, 1./Q);
CC = -U' * X;
A = lyap(AA,BBQ,CC);
V = C1 * A';


r = X-U*A;
Q = 1 ./ (c^2 + sum(r .* r,1));
L = 2* sqrt((sum(V.*V,2))); %L^{-1}
end
